﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class ExternalData
    {
        private String FormatName(String name)
        {
            if (name != null)
                return name.TrimStart().Substring(0, 1).ToUpper() + name.TrimEnd().Substring(1).ToLower();
            return "";
        }

        public void updatePhysicianList()
        {
            foreach (AdminData data in this.AdminDatas)
            {
                String phFirstName = FormatName(data.PhysicianFirstName);
                String phLastName = FormatName(data.PhysicianLastName);
                String phPhone = data.PhysicianPhone.Trim();
                Physician physician = this.DataWorkspace.ApplicationData.Physicians.Where(physicians =>
                    physicians.FirstName == phFirstName && physicians.LastName == phLastName &&
                    physicians.Phone == phPhone).FirstOrDefault();
                if (physician == null)
                {
                    Physician newPhysician = this.DataWorkspace.ApplicationData.Physicians.AddNew();
                    newPhysician.FirstName = phFirstName;
                    newPhysician.LastName = phLastName;
                    newPhysician.Phone = phPhone;
                }
            }
        }

        partial void EmrDatasDeleteSelected_CanExecute(ref bool result)
        {
            // Write your code here.
            result = this.EmrDatas.SelectedItem != null && 
                (this.EmrDatas.SelectedItem.Id == 0 || 
                (this.EmrDatas.SelectedItem.Status != null && this.EmrDatas.SelectedItem.Status != " NO_PATIENT_RECORD ")); 
        }

        partial void EmrDatasDeleteSelected_Execute()
        {
            // Write your code here.
            this.EmrDatas.DeleteSelected();
        }

    }
}
